<?
    function getMatrices($xml_file) {
        global $matrizes;
        $objeto  = new DomDocument;
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("matrices");
        if ($filho->length > 0) {
            $temp = $filho->item(0)->getelementsbytagname("matrix");
            $j = 0;
            for($i = 0; $i < $temp->length; $i++) {
                $conteudo = $temp->item($i)->getelementsbytagname("num_rows");
                if ($conteudo->length == 1) {
                $matrizes[$j] = $temp->item($i);
                $j++;
                }
            }
            return $matrizes;
        }
        else return null;
    }

    function getRows($indice) {
        global $matrizes, $r;
        $linhas = $matrizes[$indice]->getelementsbytagname("num_rows");
        $r = $linhas->item(0)->nodeValue;
        return $r;
    }

    function getCols($indice) {
        global $matrizes, $c;
        $colunas = $matrizes[$indice]->getelementsbytagname("num_cols");
        $c = $colunas->item(0)->nodeValue;
        return $c;
    }

    function getDesc($indice) {
        global $matrizes, $desc;
        $descricao = $matrizes[$indice]->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $matrizes, $com;
        $comentario = $matrizes[$indice]->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $matrizes, $ref;
        $referencia = $matrizes[$indice]->getelementsbytagname("reference");
        if ($referencia->length > 0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function getMatrix($indice) {
        global $matrizes, $M;
        $r = getRows($indice);
        $linhas = $matrizes[$indice]->getelementsbytagname("row");
        for($i = 0; $i < $linhas->length; $i++) {
            $x = $linhas->item($i)->getelementsbytagname("x");
            if ($i < $r) for($j = 0; $j < $x->length; $j++) {
                $M[$i][$j] = $x->item($j)->nodeValue;
            }
            else for($j = 0; $j < $x->length; $j++) {
                $M[$i][$j] = $x->item($j)->nodeValue;
            }
        }
        return $M;
    }


    function createMenu() {
        global $matrizes,$msg;
        for($i = 0; $i < sizeof($matrizes); $i++) {
            echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
            $temp = getRows($i); $temp1 = getCols($i);
            echo $msg->get("XML_EXIBE_MATRIZ_MSG06");
            echo "("."$temp"."x"."$temp1".")";
            echo "</u></a><p>";
        }
        return;
    }

    if (empty($xml_file) && empty($id_bib)) $id_bib = "bib_demo.xml"; 

    if ( (!isset($indice)) || ($indice == null) ) { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?inc=soma.php&modo=exemplo';
               document.myform.submit();
            }

        </script>
        <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib($xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getMatrices($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get("XML_EXIBE_MSG04")."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get("XML_EXIBE_MSG05");
                     echo "<p><input class=button type=button value=".$msg->get("XML_BACK")." onClick=history.back()>";
                }
	    } else {
                 echo $msg->get("XML_EXIBE_MSG06");
                 echo "<p><input class=button type=button value=".$msg->get("XML_BACK")." onClick=history.back()>";
	    }
 	 ?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value=<? echo $msg->get("XML_EXIBE_MSG07"); ?> onClick="history.back()">
        </form>

<? } else { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {
               document.myform.Calcular.value = 'sim';
               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=soma.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getMatrices($xml_file); ?>
        <? $rows = getRows($indice);?>
        <? $columns = getCols($indice);?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>
        <? $M = getMatrix($indice); ?>

        <? verificaBib($xml_file); ?>
        <? if (!strlen($desc) == 0) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get("XML_EXIBE_MATRIZ_MSG03"); ?> </b><? echo htmlentities(utf8_decode($desc)) ?></font>
        <? } ?>
        <p>
		<p align="left"><font face="Arial"><b><? echo $msg->get("XML_EXIBE_MATRIZ_MSG07"); ?>
        &nbsp</b>A<sub><? echo "$rows"."x"."$columns"; ?></sub><b>&nbsp + &nbsp</b>B<sub><? echo "$rows"."x"."$columns"; ?></sub></font>
		<p>
		<?//<table bordercolor="#000000" frame="border" rules="all" style="border:3px none">
		?>
		<table border="0">
		<? for ($i=0; $i < $rows; $i++) {	?>
            <tr>
            <? for ($j=0; $j<$columns; $j++) { ?>
                  <? if ($i < $rows) { ?>
   			           <td align=center valign=center width=20> <? echo $M[$i][$j] ?> </td>
			           <input type="hidden" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo $M[$i][$j] ?>">
                  <? } else { ?>
                       <td style="border:0px none">&nbsp</td>
                  <? } ?>
            <? } ?>
            <?
	    if ($i == 0)
	    	echo "<td rowspan=\"".$rows."\"><b>+</b></td>";
	    ?>
	   	    <?	for ($j=0; $j<$columns; $j++) { ?>
                <? if (!empty($M[$i+$rows][$j])) { ?>
			    <td align=center valign=center width=20> <? echo $M[$i+$rows][$j] ?> </td>
			    <input type="hidden" name="B[<? echo $i ?>][<? echo $j ?>]" value="<? echo $M[$i+$rows][$j] ?>">
                <? } else { ?>
                    <td style="border:0px none">&nbsp</td>
                <? } ?>
            <? } /* end for j */?>
		    </tr>
		<? } //end for i ?>
        </table>
        <p>
        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get("XML_EXIBE_MSG08");?></b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get("XML_EXIBE_MSG09"); ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>

        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="rows" value="<? echo $rows ?>">
        <input type="hidden" name="rows2" value="<? echo $rows2 ?>">
        <input type="hidden" name="columns" value="<? echo $columns ?>">
        <input type="hidden" name="columns2" value="<? echo $columns2 ?>">
        <input type="hidden" name="Calcular" >
	<input type="hidden" name="navegacao" value="sim">
        <input class="button" type="button" value=<? echo $msg->get("XML_EXIBE_MSG010"); ?> onClick="sendToCalculus()"><p>
        </form>
<?
	echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
	echo "<script language=\"javascript\" type=\"text/javascript\">\n";
	echo "function criarLinks(value) {\n";
	echo "document.exemplos.indice.value = value;\n";
	echo "document.exemplos.action = 'index.php?inc=soma.php&modo=exemplo'\n";
	echo "document.exemplos.submit();\n";
	echo "}\n";
	echo "</script>";
	echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
	echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
	echo "<input type=\"hidden\" name=\"indice\">";
	echo "<br><br>".$msg->get("Outros_Exemplos")."<br>";
    $objeto = new DomDocument();
    $objeto->load($xml_file);
	$filho = $objeto->getelementsbytagname("matrices");
	if ($filho->length > 0)
	{
		$temp = $filho->item(0)->getelementsbytagname("matrix");
		$j = 0;
		for($i = 0; $i < $temp->length; $i++) {
			$conteudo = $temp->item($i)->getelementsbytagname("num_rows");
			if ($conteudo->length == 1)
			{
				if ($j != 0) echo " | ";
				if ($j == $indice) echo "<b>";
				echo "<a href=\"javascript:criarLinks($j)\"><u>".($j+1)."</u></a>";
				if ($j == $indice) echo "</b>";
				$j++;
			}
		}
	}
?>

<? } ?>
